#include "keywords.h"
#include <string.h>
#include "symbol.h"
#include "def.h"

/**
 * @brief keywords名称与symbol类型对应表
 */
static const keywords_type_st keywords_type_table[] = 
{
    {"for",         symbol_type_for, 0},
    {"do",          symbol_type_do, 0},
    {"while",       symbol_type_while, 0},
    {"break",       symbol_type_break, 0},
    {"continue",    symbol_type_continue, 0},
    {"char",        symbol_type_char, 0},
    {"double",      symbol_type_double, 0},
    {"enum",        symbol_type_enum, 0},
    {"float",       symbol_type_float, 0},
    {"int",         symbol_type_int, 0},
    {"long",        symbol_type_long, 0},
    {"short",       symbol_type_short, 0},
    {"signed",      symbol_type_signed, 0},
    {"struct",      symbol_type_struct, 0},
    {"union",       symbol_type_union, 0},
    {"unsigned",    symbol_type_unsigned, 0},
    {"void",        symbol_type_void, 0},
    {"if",          symbol_type_if, 0},
    {"else",        symbol_type_else, 0},
    {"goto",        symbol_type_goto, 0},
    {"switch",      symbol_type_switch, 0},
    {"case",        symbol_type_case, 0},
    {"default",     symbol_type_default, 0},
    {"return",      symbol_type_return, 0},
    {"auto",        symbol_type_auto, 0},
    {"extern",      symbol_type_extern, 0},
    {"register",    symbol_type_register, 0},
    {"static",      symbol_type_static, 0},
    {"const",       symbol_type_const, 0},
    {"sizeof",      symbol_type_sizeof, 0},
    {"typedef",     symbol_type_typedef, 0},
    {"volatile",    symbol_type_volatile, 0},
};

/**
 * @brief 根据标识符名称，判断标识符是否为关键字
 * 
 * @param symbol_name 标识符名称
 * @param symbol_len 标识符名称长度
 * @return uint32_t 返回符号类型
 */
uint32_t keywords_get_symbol_type(
    uint8_t *symbol_name,
    uint32_t symbol_len)
{
    for(uint32_t index = 0; index < ARRSIZE(keywords_type_table); index++)
    {
        const keywords_type_st *keyword = &(keywords_type_table[index]);
        uint32_t keyword_len = strlen(keyword->name);
        if(keyword_len != symbol_len)
        {
            continue;
        }

        if(0 == memcmp(keyword->name, symbol_name, keyword_len))
        {
            return keyword->type;
        }
    }
    return symbol_type_id;
}

